<?php


namespace app\api\controller;
use think\App;
use think\Controller;
use think\Exception;
use app\common\lib\ErrorCode;
use think\facade\Session;
use app\api\model\User;
use app\common\facade\WebSocketToken;

class Base extends Controller
{
    protected $fd = 0;
    protected $user_id = 0;
    protected $user_name = 0;

    public function __construct(App $app = null)
    {
        parent::__construct($app);
        !$this->user_id && Session::get('user_id') && $this->user_id =  Session::get('user_id');
        !$this->user_name && Session::get('user_name') && $this->user_id =  Session::get('user_name');
    }

    protected function check_user_info(){
        if(!Session::get('user_id'))
            throw new Exception("登录过期!", ErrorCode::$LOGIN_ERROR);

        $user = User::get(Session::get('user_id'));
        if(!$user)
            throw new \Exception("无效的用户", ErrorCode::$LOGIN_ERROR);

        $this->user_id = $user->id;
        $this->user_name = $user->name;

        $this->fd = WebSocketToken::get_fd($this->user_id);
    }
    protected function set_user_info($user){
        Session::set('user_id',$user['id']);
        Session::set('user_name',$user['name']);
    }
}